import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  volunteerList: {},
  currentVolunteer: {},
  searchVolunteerCondition: {
    pageSize: 10
  }
};

const getters = {
  volunteerList: state => state.volunteerList,
  currentVolunteer: state => state.currentVolunteer,
  searchVolunteerCondition: state => state.searchVolunteerCondition
};

const actions = {
  getVolunteerPageS({ commit }, param) {
    return Api.getVolunteerPage(param).then(ret => {
      commit(types.UPDATE_VOLUNTEER_LIST, ret.data.data);
      return ret;
    });
  },
  getVolunteerS({ commit }, id) {
    return Api.getVolunteer({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_VOLUNTEER, ret.data.data);
      return ret;
    });
  },
  addOrUpdateVolunteerS({ commit }, payload) {
    return Api.addOrUpdateVolunteer({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteVolunteerS({ commit }, param) {
    return Api.deleteVolunteer(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_VOLUNTEER](state, payload) {
    state.currentVolunteer = {
      birth: "",
      intent: "",
      message: "",
      name: "",
      phone: "",
      sex: ""
    };
  },
  [types.UPDATE_CURRENT_VOLUNTEER](state, payload) {
    console.log(payload);
    state.currentVolunteer = payload;
  },
  [types.UPDATE_VOLUNTEER_LIST](state, payload) {
    state.volunteerList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
